﻿using HWCDNLogAnalysis.Model;
using HWCDNLogAnalysis.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HWCDNLogAnalysis
{
    public partial class frmAnalysis : Form
    {
        public class resultAnalysis
        {
            public string id { get; set; } = "";
            public string key { get; set; } = "";
            public int value { get; set; } = 0;
            public string percent { get; set; } = "";
            public List<BrowseLog> resultList { get; set; }= new List<BrowseLog>();
        }
        public List<BrowseLog> resultList { get; set; }=new List<BrowseLog>();
        public List<resultAnalysis> _resultAnalysis { get; set; } = new List<resultAnalysis>();
        public List<resultAnalysis> _resultHit { get; set; } = new List<resultAnalysis>();
        public frmAnalysis(List<BrowseLog> _resultList)
        {
            InitializeComponent();
            resultList = _resultList;
        }
        private void frmAnalysis_Load(object sender, EventArgs e)
        {
            getStatusGroup();
            getUrlGroup();
            getHitGroup();
        }
        private void tabAnalysis_Selected(object sender, TabControlEventArgs e)
        {
            
        }
        private void dgvAnalysis_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var _index=e.RowIndex;
            var _rs = _resultAnalysis[_index].resultList;
            tabAnalysis.SelectedTab = tabPage3;
            dgvLog.DataSource= ListUtils.ToDataTable(_rs);
        }
        private void dgvHit_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var _index = e.RowIndex;
            var _rs = _resultHit[_index].resultList;
            tabAnalysis.SelectedTab = tabPage3;
            dgvLog.DataSource = ListUtils.ToDataTable(_rs);
        }
        private void getStatusGroup()
        {
            var _groups = resultList.GroupBy(x => x.Status);
            var _lst = new List<resultAnalysis>();
            int i = 0;
            foreach (var group in _groups)
            {
                i++;
                var x = new resultAnalysis();
                x.id = i.ToString();
                x.key = group.Key;
                x.value = group.Count();
                x.percent = (Math.Round((group.Count() * 1.0 / resultList.Count() * 1.0) * 100, 2).ToString()) + "%";
                x.resultList = group.ToList();
                _lst.Add(x); 
            }
            _resultAnalysis = _lst;
            dgvAnalysis.DataSource = ListUtils.ToDataTable(_lst);
        }
        private void getUrlGroup()
        {
            var _groups = resultList.GroupBy(x => x.Uri);
            var _lst = new List<resultAnalysis>();
            int i = 0;
            foreach (var group in _groups)
            {
                i++;
                var x = new resultAnalysis();
                x.id = i.ToString();
                x.key = group.Key;
                x.value = group.Count();
                x.percent = (Math.Round((group.Count() * 1.0 / resultList.Count() * 1.0) * 100, 2).ToString()) + "%";
                x.resultList = group.ToList();
                _lst.Add(x);
            }
            dgvRank.DataSource = ListUtils.ToDataTable(_lst);
        }
        private void getHitGroup()
        {
            var _groups = resultList.GroupBy(x => x.CDNHit);
            var _lst = new List<resultAnalysis>();
            int i = 0;
            
            foreach (var group in _groups)
            {
                i++;
                var x = new resultAnalysis();
                x.id = i.ToString();
                x.key = group.Key;
                x.value = group.Count();
                x.percent = (Math.Round((group.Count() * 1.0 / resultList.Count() * 1.0) *100,2).ToString())+"%";
                x.resultList = group.ToList();
                _lst.Add(x);
            }
            _resultHit = _lst;
            dgvHit.DataSource = ListUtils.ToDataTable(_lst);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FileUtils.dataGridViewToCSV(dgvLog);
        }
    }
}
